package com.gdut.huayan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gdut.huayan.dao.ConversionActivityMapper;
import com.gdut.huayan.dao.ConversionMapper;
import com.gdut.huayan.entity.dto.PageQuery;
import com.gdut.huayan.entity.po.Conversion;
import com.gdut.huayan.entity.po.ConversionActivity;
import com.gdut.huayan.service.ConversionActivityService;
import com.gdut.huayan.service.exception.BaseException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;

@Service
public class ConversionActivityServiceImpl extends ServiceImpl<ConversionActivityMapper, ConversionActivity> implements ConversionActivityService {

    @Autowired
    private ConversionMapper conversionMapper;

    @Override
    public boolean save(ConversionActivity conversionActivity) {
        conversionActivity.setStatus(1);
        if(conversionActivity.getType().equals(3) || conversionActivity.getType().equals(1)){
            if(conversionActivity.getSanGuiStartTime() == null || conversionActivity.getSanGuiEndTime() == null || conversionActivity.getSanGuiConversionTime() == null){
                throw new BaseException(4000,"三皈时间参数未填写完全");
            }
            if(conversionActivity.getSanGuiStartTime().isAfter(conversionActivity.getSanGuiEndTime())){
                throw new BaseException(4000,"三皈开始时间晚于结束时间");
            }
        }
        if(conversionActivity.getType().equals(3) || conversionActivity.getType().equals(2)) {
            if (conversionActivity.getWuJieStartTime() == null || conversionActivity.getWuJieEndTime() == null || conversionActivity.getWuJieConversionTime() == null) {
                throw new BaseException(4000, "五戒时间参数未填写完全");
            }
            if (conversionActivity.getWuJieStartTime().isAfter(conversionActivity.getWuJieEndTime())) {
                throw new BaseException(4000, "五戒开始时间晚于结束时间");
            }
        }
        return super.save(conversionActivity);
    }

    @Override
    public PageInfo<ConversionActivity> getAll(PageQuery pageQuery, Integer status, Integer type) {
        PageHelper.startPage(pageQuery.getPage(),pageQuery.getSize());
        QueryWrapper<ConversionActivity> queryWrapper = new QueryWrapper<>();
        if(status != null){
            queryWrapper.eq("status",status);
        }
        if(type != null){
            queryWrapper.eq("type",type);
        }
        if ("ASC".equals(pageQuery.getOrder())) {
            queryWrapper.orderByAsc(pageQuery.getOrderKey().split(";"));
        }
        if ("DESC".equals(pageQuery.getOrder())) {
            queryWrapper.orderByDesc(pageQuery.getOrderKey().split(";"));
        }
        return new PageInfo<>(baseMapper.selectList(queryWrapper));
    }

    @Override
    public boolean updateById(ConversionActivity conversionActivity){
        conversionActivity.setImageFileId(null);
        if(isParticipateActivity(conversionActivity.getId())){
            //若已有用户参加，则以下数据不能修改
            ConversionActivity updateActivity = new ConversionActivity();
            updateActivity.setId(conversionActivity.getId());
            updateActivity.setFrontName(conversionActivity.getFrontName());
            updateActivity.setStatus(conversionActivity.getStatus());
            updateActivity.setPageJson(conversionActivity.getPageJson());
            conversionActivity = updateActivity;
        }
        return super.updateById(conversionActivity);
    }

    @Override
    public boolean removeById(Serializable id){
        if(isParticipateActivity(id)){
            throw new BaseException(4000,"该活动已有信众参加，无法删除");
        }
        return super.removeById(id);
    }

    //判断是否有人参加活动
    private boolean isParticipateActivity(Serializable id){
        if(id == null){
            throw new BaseException(4000,"请输入活动id");
        }
        Integer count = new LambdaQueryChainWrapper<>(conversionMapper).eq(Conversion::getActivityId, id).count();
        return count != null && count > 0;
    }
}
